package com.frontResponse.controller;

import com.alibaba.fastjson.JSON;
import com.frontResponse.service.impl.VideoServiceImpl;
import com.javasm.entity.ReturnCode;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author WangJie
 * @version 1.0
 * @Description: 视频模块的servlet
 * @date: 2020/7/23 21:50
 * @since JDK 1.8
 */
@WebServlet("/video")
public class VideoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        List<Map<String, Object>> data = null;
        Map<String, Object> returnData = new HashMap<>();
        Map<String, Object> TVInfo = null; // 查询剧集信息的集合
        if ("getVideoTopSort".equals(method)) {
            data = getVideoTopSort(req, resp);

        } else if ("getVideoSecondSortByTsvid".equals(method)) {
            data = getVideoSecondSortByTsvid(req, resp);

        } else if ("getVideoByPage".equals(method)) {
            data = getVideoByPage(req, resp);

        } else if ("getVideoSoloByVid".equals(method)) {
            data = getVideoSoloByVid(req, resp);

        } else if ("getTVPlayByVid".equals(method)) {
            TVInfo = getTVPlayByVid(req, resp);
            returnData.put("TVInfo", TVInfo);
        }
        // 返回值判断
        if (data == null && TVInfo == null) {
            returnData.put("code", ReturnCode.ERROR.getCode());
            returnData.put("message", ReturnCode.ERROR.getMsg());
        } else {
            returnData.put("code", ReturnCode.SUCCESS.getCode());
            returnData.put("message", ReturnCode.SUCCESS.getMsg());
            returnData.put("data", data);
        }
        PrintWriter pw = resp.getWriter();
        pw.print(JSON.toJSONString(returnData));
        pw.flush();
        pw.close();
    }

    // 通过视频id查询对应的剧集信息
    private Map<String, Object> getTVPlayByVid(HttpServletRequest req, HttpServletResponse resp) {
        String vid = req.getParameter("vid");
        return new VideoServiceImpl().selectTVPlayByVid(vid);
    }

    // 通过视频id查询对应电影的信息
    private List<Map<String, Object>> getVideoSoloByVid(HttpServletRequest req, HttpServletResponse resp) {
        String vid = req.getParameter("vid");
        return new VideoServiceImpl().selectVideoSoloByVid(vid);
    }

    // 根据二级分类查询具体视频信息
    private List<Map<String, Object>> getVideoByPage(HttpServletRequest req, HttpServletResponse resp) {
        String ssvid = req.getParameter("ssvid");
        int page = Integer.parseInt(req.getParameter("page"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        return new VideoServiceImpl().selectVideoInfoByPage(ssvid, page, pageSize);
    }

    // 根据一级分类id查询二级分类
    private List<Map<String, Object>> getVideoSecondSortByTsvid(HttpServletRequest req, HttpServletResponse resp) {
        String tsvid = req.getParameter("tsvid");
        return new VideoServiceImpl().selectVideoSecondSortByTsvid(tsvid);
    }

    // 查询视频一级分类
    private List<Map<String, Object>> getVideoTopSort(HttpServletRequest req, HttpServletResponse resp) {
        return new VideoServiceImpl().selectVideoTopSort();
    }
}
